package com.game.alert
{
	
	
	
	import avmplus.getQualifiedClassName;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import flash.utils.getDefinitionByName;

	/**
	 * 
	 * @author lilh
	 * 用法：AlertCtrl.getInstance.showYse()|showYesNo()|showCancel();
	 * 
	 */	
	public class AlertCtrl extends Sprite
	{
		private var _defaultMc:AlertMc;
		
		
		private var typeName:String;//获取全名
		private var type:Class ;
		
		private var _singleY:int = 60;
		private var _singleHeight:int = 22;
		private var _multiY:int = 50;
		private var _multiHeight:int = 60;
		private var _contentW:int = 500;
		
		
		
		private var _gameW:uint;
		private var _gameH:uint;
		
		private var _content:TextField;
		private var _yesBtn:SimpleButton;
		private var _noBtn:SimpleButton;
		private var _cancelBtn:SimpleButton;
		
		
		private var _parent:Sprite;
		private var _alertMc:AlertMc;
		private var _alertBgSpt:Sprite;
		
		
		private var _yesLabel:String = "Yes";
		private var _noLabel:String = "No";
		private var _cancelLabel:String = "Cancel";
		
		private var _firstButton:uint = 1;
		private var _visible:Boolean = false;
		private var _closeHander:Function;
		private var _timerIn:Timer;
		private var _listIn:Array;
		private var _timerOut:Timer;
		private var _listOut:Array;
		
		private var _alertMcList:Array=[];;
		private static var _obj:AlertCtrl;
		
		public function AlertCtrl()
		{
			
		}
		
		private function initDefaultAlert():void
		{
			
			_defaultMc =new type() as AlertMc;
			
			
			
			
			_yesBtn = _defaultMc["yesBtn"];
			_noBtn = _defaultMc["noBtn"]
			_cancelBtn = _defaultMc["cancelBtn"];
			_content = _defaultMc["content"];
			
			settleButton(_yesBtn, _yesLabel);
			settleButton(_noBtn, _noLabel);
			settleButton(_cancelBtn, _cancelLabel);
			_contentW = _content.width;
			
		}
		private function initAlertBg():void{
			if(_alertBgSpt!=null){
				_parent.addChild(_alertBgSpt);
				return;
			}
			_alertBgSpt=new Sprite();
			_alertBgSpt.graphics.clear();
			_alertBgSpt.graphics.beginFill(0, 0.3);
			_alertBgSpt.graphics.drawRect(0, 0, _gameW, _gameH);
			_alertBgSpt.graphics.endFill();
			_parent.addChild(_alertBgSpt);
		}
		
		private function show(msg:String, type:uint = 1, closeFunc:Function = null):void
		{
			initDefaultAlert();
			_alertMc=_defaultMc;
			if(_alertMc._width==0){
				_alertMc._width=_alertMc.width;
			}
			if(_alertMc._height==0){
				_alertMc._height=_alertMc.height;
			}
			formatText(msg);
			_closeHander = closeFunc;
			buttonState(_yesBtn, _yesLabel);
			buttonState(_noBtn, _noLabel);
			buttonState(_cancelBtn, _cancelLabel);
			var buttonArr:Array = [];
			switch(type)
			{
				case 1:
				{
					yesVisible = true;
					buttonArr.push(_yesBtn);
					_firstButton = AlertButtonType.Yes;
					break;
				}
				
				case 2:
				{
					yesVisible = true;
					noVisible = true;
					buttonArr.push(_yesBtn, _noBtn);
					_firstButton = AlertButtonType.Yes;
					break;
				}
				
				case 3:
				{
					yesVisible = true;
					noVisible = true;
					cancelVisible = true;
					buttonArr.push(_yesBtn, _noBtn, _cancelBtn);
					_firstButton = AlertButtonType.Yes;
					break;
				}
				default:
				{
					yesVisible = true;
					buttonArr.push(_yesBtn);
					_firstButton = AlertButtonType.Yes;
					break;
				}
					
			}
			var gap:int = 50;
			var allW:int = (buttonArr.length - 1) * gap + buttonArr[0].width * buttonArr.length;
			var startX:int = (_alertMc.width - allW) / 2;
			
			for(var i:int = 0; i < buttonArr.length; i++)
			{
				buttonArr[i].x = startX + i * (buttonArr[i].width + gap);
			}
			
			setShowAlert();
		}
		
		private function setShowAlert():void{
			_alertMcList.push(_alertMc);
			initAlertBg();
			if (_parent)
			{
				_parent.addChild(_alertMc);
			}
			
			reposition();
			
			startFadeIn();
			
		}
		
		private function formatText(msg:String):void
		{
			_content.htmlText = msg;
			_content.autoSize = TextFieldAutoSize.LEFT;
			_content.multiline = false;
			_content.wordWrap = false;
			var format:TextFormat = new TextFormat();
			format.leading = 12;
			format.align = "center";
			_content.setTextFormat(format);
			var bool:Boolean = /\r|\n|\<br\>/.test(msg);
			if (_content.width > _contentW || bool)
			{
//				format.align = bool ? ("center"):("left");
				_content.y = _multiY;
				_content.height = _multiHeight;
				_content.multiline = true;
				_content.wordWrap = true;
			}
			else
			{
				_content.y = _singleY;
				_content.height = _singleHeight;
			}
			_content.autoSize = "none";
			_content.width = _contentW;
			_content.setTextFormat(format);
		}
		
		
		private function settleButton(btn:SimpleButton, label:String):void
		{
			btn.visible = true;
//			btn.buttonMode = true;
//			btn.txt.mouseEnabled = false;
			btn.addEventListener(MouseEvent.CLICK, onClickHandler);
		}
		
		private function onClickHandler(e:MouseEvent):void
		{
			var type:int = AlertButtonType.Yes;
			if (e.currentTarget == _noBtn)
			{
				type = AlertButtonType.No;
			}
			else if (e.currentTarget == _cancelBtn)
			{
				type = AlertButtonType.Cancel;
			}
			hide();
			if (_closeHander is Function)
			{
				_closeHander(type);
			}
		}
		
		private function set yesVisible(bool:Boolean):void
		{
			_yesBtn.visible = bool;
		}
		
		private function set noVisible(bool:Boolean):void
		{
			_noBtn.visible = bool;
		}
		
		private function set cancelVisible(bool:Boolean):void
		{
			_cancelBtn.visible = bool;
		}
		
		private function buttonState(btn:SimpleButton, label:String):void
		{
			btn.visible = false;
//			btn["txt"].text = label;
		}
		
		
		private function startFadeIn():void
		{
			
			if(!_alertMc.isFade){
				return;
			}
			_alertMc.visible = false;
			if (_parent == null)
			{
				return;
			}
			if (_timerIn == null)
			{
				_timerIn = new Timer(10);
				_timerIn.addEventListener(TimerEvent.TIMER, onFadeIn);
				onFadeIn();
			}
			var bmd:BitmapData = new BitmapData(_alertMc._width, _alertMc._height, true, 0);
			bmd.draw(_alertMc);
			var bm:Bitmap = new Bitmap(bmd);
			_parent.addChild(bm);
			bm.scaleX = 0.5;
			bm.scaleY = 0.5;

			setCenter(bm);
			_listIn.push(bm);
			if (_listIn.length == 1)
			{
				_timerIn.start();
			}
		}
		
		private function onFadeIn(event:TimerEvent=null):void
		{
			var bm:Bitmap = null;
			var index:int = _listIn.length - 1;
			while (index > -1)
			{
				bm = _listIn[index];
				if (bm.scaleX < 1)
				{
					bm.scaleX += 0.1;
					bm.scaleY += 0.1;
					setCenter(bm);
				}
				if (bm.scaleX > 0.9)
				{
					_parent.removeChild(bm);
					_listIn.splice(index, 1);
					_alertMc.visible = true;
				}
				index--;
			}
			if (_listIn.length == 0)
			{
				_timerIn.stop();
			}
		}
		
		private function startFadeOut():void
		{
			
			if(!_alertMc.isFade){
				return;
			}
			if (_parent == null ||_alertMc.parent == null)
			{
				return;
			}
			if (_timerOut == null)
			{
				_timerOut = new Timer(10);
				_timerOut.addEventListener(TimerEvent.TIMER, onFadeOut);
				onFadeOut();
			}
			var bmd:BitmapData = new BitmapData(_alertMc._width, _alertMc._height, true, 0);
			bmd.draw(_alertMc);
			var bm:Bitmap = new Bitmap(bmd);
			setCenter(bm);
			_parent.addChild(bm);
			bm.x = x;
			bm.y = y;
			_listOut.push(bm);
			if (_listOut.length == 1)
			{
				_timerOut.start();
			}
		}
		
		private function onFadeOut(event:TimerEvent=null):void
		{
			var bm:Bitmap = null;
			var index:int = _listOut.length - 1;
			while (index > -1)
			{
				
				bm = _listOut[index];
				_listOut[index].alpha = bm.alpha - 0.15;
				if (bm.scaleX - 0.08 > 0)
				{
					bm.scaleX = bm.scaleX - 0.08;
					bm.scaleY = bm.scaleY - 0.08;
					setCenter(bm);
				}
				if (bm.alpha <= 0)
				{
					bm.parent.removeChild(bm);
					_listOut.splice(index, 1);
				}
				index--;
			}
			if (_listOut.length == 0)
			{
				_timerOut.stop();
			}
		}
		
		private function setCenter(disObj:DisplayObject,w:uint=0,h:uint=0):void{
			if(w==0){
				w=disObj.width;
			}
			if(h==0){
				h=disObj.height;
			}
			
			disObj.x= Math.floor((_gameW - w) / 2);
			disObj.y= Math.floor((_gameH - h) / 2);
		}
		private function reposition():void
		{
			
			if (_parent == null || _alertMc.parent == null)
				return;
			
			setCenter(_alertMc,_alertMc._width,_alertMc._height);
			
		}
		
		private function hide():void
		{
			startFadeOut();
			
			
			if (_alertMc.parent)
			{
				_alertMc.parent.removeChild(_alertMc);
				_alertMc=null;
			}
			_alertMcList.pop();
			
			
			if(_alertMcList.length==0){
				if (_alertBgSpt.parent)
				{
					_alertBgSpt.parent.removeChild(_alertBgSpt);
				}
				return;
			}
			_alertMc=_alertMcList[_alertMcList.length-1];
			if (_parent)
			{
				_parent.addChild(_alertMc);
			}
			
		}
		
		private function defaultCloseEvent():void
		{
			hide();
			if (_closeHander is Function)
			{
				_closeHander(_firstButton);
			}
		}
		
		private function updateContent(msg:String):void
		{
			formatText(msg);
		}
		
		public function set oParent(sp:Sprite):void
		{
			_parent = sp;
		}
		
		public static function getInstance():AlertCtrl
		{
			if (!_obj)
			{
				_obj = new AlertCtrl();
				_obj.reposition();
			}
			return _obj;
		}
		
		public function init(MC:AlertMc,W:uint,H:uint):void
		{
			 typeName = getQualifiedClassName(MC);//获取全名
			 type = getDefinitionByName(typeName) as Class;//获取Class
			
			_gameW=W;
			_gameH=H;
			_listIn = [];
			_listOut = [];
			
		}
		
		public function showAlertMc(alertMc:AlertMc):void{
			_alertMc=alertMc;
			_alertMc.hideFun=hide;
			;
			if(_alertMc._width==0){
				_alertMc._width=_alertMc.width;
			}
			if(_alertMc._height==0){
				_alertMc._height=_alertMc.height;
			}
			
			setShowAlert();
		}
		/**
		 * 
		 * @param msg  内容
		 * @param clickFunc 点击返回函数
		 * @param yseLabel 按钮label
		 * 
		 */		
		public function showYes(msg:String, clickFunc:Function = null, yseLabel:String = "确定"):void
		{
			_yesLabel = yseLabel
			show(msg, 1, clickFunc);
		}
		/**
		 * 显示两个按钮
		 * @param msg
		 * @param clickFunc
		 * @param yseLabel
		 * @param noLabel
		 * 
		 */		
		public function showYesNo(msg:String, clickFunc:Function = null, yseLabel:String = "确定", noLabel:String = "取消"):void
		{
			_yesLabel = yseLabel;
			_noLabel = noLabel;
			show(msg, 2, clickFunc);
		}
		/**
		 * 显示三个按钮
		 * @param msg
		 * @param clickFunc
		 * @param yseLabel
		 * @param noLabel
		 * @param cancelLabel
		 * 
		 */		
		public function showCancel(msg:String, clickFunc:Function = null, yseLabel:String = "确定", noLabel:String = "不了", cancelLabel:String = "取消"):void
		{
			_yesLabel = yseLabel;
			_noLabel = noLabel;
			_cancelLabel = cancelLabel;
			show(msg, 3, clickFunc);
		}
		
		
		
	}
}
